<?php  
include_once '/var/www/html/new/com.inc.php';
include("common/header.html");
include("common/left.html");
include("common/page.php");
include("common/billcommon.php");
$r=array();
$status=array();
$where="where aid=$memberID";

///print_r($fids);
//exit;
//上个账单结束时间
$sql="select etime from uu_bill where aid=$memberID order by id desc limit 1";
$GLOBALS['le']->query($sql);
$GLOBALS['le']->fetch_assoc();
$ltime=$GLOBALS['le']->f('etime');
$btime=date("Y-m-d",strtotime("$ltime +1 day")).' 00:00:00';
$etime=date("Y-m-d").' 23:59:59';//当前时间

foreach($fids as $fid){
	$f['fid']=$fid;
	$r[]=$f;
}

//找出在账单期间结算的订单
foreach($r as $keys=>$row){
	$obj=$s->Order_Globle_Search("",$row['fid'],"","","","","","","","",$btime,$etime,"","","","1|6","0|1|2","",1,1,0,99999,"0","0","","0","","0","","",$memberID,0,"","","","");
	$or=simplexml_load_string($obj);
	foreach($or as $val){
		$order[]=(string)$val->UUordernum;
	}
	//print_r($order);
	$r[$keys]['order']=$order;
	unset($order);
}
//print_r($r);
//exit;
//找出用授信支付的订单
foreach($r as $key=>$v){
	//print_r($v);
	for($i=0;$i<count($v['order']);$i++){
		$sel="select * from pft_member_journal where orderid='".$v['order'][$i]."' and fid='".$v['fid']."' and aid='".$memberID."' and daction=1 and dtype=0 and ptype=2 limit 1";
		$GLOBALS['le']->query($sel);
		$row=$GLOBALS['le']->fetch_assoc();
		if($row){
			$order[]=$v['order'][$i];
		}
	}
	if($order){
		$r[$key]['order']=$order;
	}else{
		$r[$key]['order']="";
	}
	unset($order);
}
//print_r($r);
//exit;
//将没有订单的商户过滤掉
$arr=array();
foreach($r as $row){
	if($row['order']!=""){
		$row['orders']="";
		$row['tnums']="";
		$row['money']="";
		$row['aid']=$memberID;
		$row['btime']=$btime;
		$row['etime']=$etime;
		$arr[]=$row;
	}
}
$r=$arr;
//print_r($r);
//exit;
//根据订单号查询具体的订单信息
foreach($r as $ky=>$rw){
	//echo $ky;
	for($i=0;$i<count($rw['order']);$i++){
		$xml=$s->Order_Globle_Search("",$rw['fid'],"","","","","","","","",$rw['btime'],$rw['etime'],
		$rw['order'][$i],"","","1|6","0|1|2","",1,1,0,9999,"0","0","2","0","","0","","",$rw['aid'],0,0,0,"","");
		$xl[$ky][]=simplexml_load_string($xml);
	}
}
//print_r($xl);
//exit;

//应收款账单和应付款账单的结算价一样，用另一接口查分销价
if($xl){
	foreach($xl as $key=>$val){
		//if($key==2){
		foreach($val as $k=>$v){
			if((string)$v->Rec->UUaids==0){
					//echo 111;
				$r[$key]['orders']=$r[$key]['orders']+1;
				$r[$key]['tnums']=$r[$key]['tnums']+(string)$v->Rec->UUtnum;
				$r[$key]['money']=$r[$key]['money']+(string)$v->Rec->UUtotalmoney;
				}else{
					$buyid=(string)$v->Rec->UUbuyid;
					$sql="select account from pft_member where id='$buyid' limit 1";
					$GLOBALS['le']->query($sql);
					$GLOBALS['le']->fetch_assoc();
					$ac1=$GLOBALS['le']->f('account');
					$pid=(string)$v->Rec->UUpid;
					$date=(string)$v->Rec->UUordertime;
					$mode=1;
					$ptype=0;
					$get_storage=0;
					$m=(string)$v->Rec->UUsellid;
					$date=substr($date,0,10);
					$tprice=$s->Dynamic_Price_And_Storage($ac1,$pid,$date,$mode,$ptype,$get_storage,$m,'','');
					$tprice=@simplexml_load_string($tprice);
					$v->Rec->UUtprice=$tprice->Rec->UUsprice;
					$v->Rec->UUtotalmoney=(string)$tprice->Rec->UUsprice*(string)$v->Rec->UUtnum;
					$r[$key]['orders']+=1;
					$r[$key]['tnums']+=(string)$v->Rec->UUtnum;
					$r[$key]['money']+=(string)$v->Rec->UUtotalmoney;
				}
		}
		//}
	}
}
//print_r($r);
$count=count($r);
$h3Title="应收款账单";
$about='get';
?>
<script type="text/javascript" src="js/Calendar.js"></script>
<link rel="stylesheet" href="css/getbill.css"/>
<link rel="stylesheet" href="css/page.css"/>
<div class="clay">
	<h3 class="mc_title">
		<span class="mctit_1"><span class="mctit_2"><?=$h3Title?></span></span>
	</h3>
	<ul class="navi">
		<li><a href="" class="settle current">未出收款账单</a></li>
		<li><a href="settle_config.html?about=<?=$about?>">结算周期配置</a></li>
		<?php if($showOfflineFlag===true):?>
        <li><a href="bill_offline.html?about=<?=$about?>">现场支付账单</a></li>
        <?if(!$access['yd'] || !($_SERVER['SCRIPT_URL']=='/paybill.html')){?>
        <li><a href="bill_offline.html?about=<?=$about?>&btype=1">在线支付账单</a></li>
        <?}?>
        <?php endif;?>
	</ul>
	<div class="settleContain">
		<!--<div class="contain">
			<form action="" class="search">
				<div class="box">
					<span>
						<input type="submit" class="sub" value="查 询"/>
						<label>出账日期:</label>
						<input type="text" name="btime" class="time" value="<?=$_REQUEST['btime']?>" onmouseup="SelectDate(this,'yyyy-MM-dd',0,0)"/>
						<label>至</label>
						<input type="text" name="etime" class="time" value="<?=$_REQUEST['etime']?>" onmouseup="SelectDate(this,'yyyy-MM-dd',0,0)"/>
					</span>
					<span class="status">
						<label>结算状态:</label>
						<input type="checkbox" name="status[]" <?if(in_array(0,$status)) echo "checked";?> value="0"/> 未结算
						<input type="checkbox" name="status[]" <?if(in_array(1,$status)) echo "checked";?> value="1"/> 结算中
						<input type="checkbox" name="status[]" <?if(in_array(2,$status)) echo "checked";?> value="2"/> 已结算
					</span>
				</div>
			</form>
		</div>-->
		<?if(count($r)>0):?>
		<div class="content">
			<table>
				<tr>
					<?if($about=="get"){?>
					<th class="tl">分销商</th>
					<?}else{?>
					<th class="tl">供应商</th>
					<?}?>
					<th>出账日期</th>
					<th>账单期间</th>
					<th>账单明细</th>
					<th>订单数</th>
					<th>票数</th>
					<th>总金额(元)</th>
					<!--<th>结算状态</th>
					<th>操作</th>-->
				</tr>
				<?foreach($r as $row):?>
				<tr>
					<?if($about=="get"):?>
					<td class="tl"><?=$dnames[$row['fid']]?></td>
					<?else:?>
					<td class="tl"><?=$dnames[$row['aid']]?></td>
					<?endif;?>
					<td><?=date("Ymd",strtotime($row['etime']))?></td>
					<td>
						<span title="<?=$row['btime']?>"><?=substr($row['btime'],0,10)?></span>
						至
						<span title="<?=$row['etime']?>"><?=substr($row['etime'],0,10)?></span>
					</td>
					<td>
						<a class="fdetail" target="_blank" href="bill_count.html?id=<?=$row['id']?>">汇总</a>
						|
						<a class="fdetail" target="_blank" href="bill_detail.html?id=<?=$row['id']?>">明细</a>
					</td>
					<td><?=$row['orders']?></td>
					<td><?=$row['tnums']?></td>
					<td><?=$row['money']/100?></td>
					<!--<td><?=$statusT[$row['status']]?></td>
					<td>
						<a  class="billalter" href="call/handle.php?from=bill_alter&act=complete&id=<?=$row['id']?>">完成</a>
					</td>-->
				</tr>
				<?endforeach;?>
			</table>
		</div>
		<?endif;?>
	</div>
	<?if($count!=0):page($count,$currentPage,$pageSize);endif;?>
</div>
<script type="text/javascript" src="js/fsettle.js"></script>